//package com.nbcb.application.core.config;
//
//import com.netflix.discovery.EurekaClient;
//
//import org.slf4j.MDC;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import javax.inject.Provider;
//import java.util.Comparator;
//import java.util.List;
//
//@Configuration
//public class GloSeqServerConfigration extends RibbonClientConfiguration {
//
//    @Value("${ribbon.eureka.approximateZoneFromHostname:false}")
//    private boolean approximateZoneFromHostname = false;
//
//    @Value("${ribbon.client.name:MWEDMP-INTERNET}")
//    private String serviceId = "client";
//    @Value("${ribbon.client.name1:MWEDMP-UNIFY}")
//    private String serviceId1 = "client";
//
//    @Autowired
//    private PropertiesFactory propertiesFactory;
//
//
//    @Bean
//    public IRule ribbonRule(IClientConfig config) {
//        return new GloSeqRule();
//    }
//
//    @Bean
//    public ServerList<?> ribbonServerList(IClientConfig config, Provider<EurekaClient> eurekaClientProvider) {
//        if (this.propertiesFactory.isSet(ServerList.class, serviceId)) {
//            return this.propertiesFactory.get(ServerList.class, config, serviceId);
//        }
//        DiscoveryEnabledNIWSServerList discoveryServerList = new DiscoveryEnabledNIWSServerList(
//                config, eurekaClientProvider);
//        GloSeqServerList serverList = new GloSeqServerList(
//                discoveryServerList, config, this.approximateZoneFromHostname);
//        return serverList;
//    }
//
//    public static class GloSeqRule extends ZoneAvoidanceRule {
//
//        @Override
//        public Server choose(Object key) {
//            int index = Character.getNumericValue(MDC.get("transNo").charAt(7));
//            BaseLoadBalancer lb = (BaseLoadBalancer)getLoadBalancer();
//            //如果无法注入ServerList,可以每次调用时更新微服务数,不过效率低
///*        if(lb.getAllServers().size() != snowFlake.getModulo()){
//            snowFlake.setModulo(lb.getAllServers().size());
//            List<Server> allServers = lb.getAllServers();
//            allServers.sort(Comparator.comparing(Server::getHost).thenComparing(Server::getPort));
//            lb.setServersList(allServers);
//        }*/
//            List<Server> eligible = getPredicate().getEligibleServers(lb.getAllServers(), key);
//            if (eligible.size() == 0) {
//                return null;
//            }
//            return eligible.get(index);
//        }
//    }
//
//    public static class GloSeqServerList extends DomainExtractingServerList {
//
//        @Autowired
//        SnowFlake snowFlake;
//
//        public GloSeqServerList(ServerList<DiscoveryEnabledServer> list, IClientConfig clientConfig, boolean approximateZoneFromHostname) {
//            super(list, clientConfig, approximateZoneFromHostname);
//        }
//
//        @Override
//        public List<DiscoveryEnabledServer> getInitialListOfServers() {
//            List<DiscoveryEnabledServer> servers = super.getInitialListOfServers();
//            servers.sort(Comparator.comparing(Server::getHost).thenComparing(Server::getPort));
//            snowFlake.setModulo(servers.size()>0?servers.size():1);
//            return servers;
//        }
//
//        @Override
//        public List<DiscoveryEnabledServer> getUpdatedListOfServers() {
//            List<DiscoveryEnabledServer> servers = super.getUpdatedListOfServers();
//            servers.sort(Comparator.comparing(Server::getHost).thenComparing(Server::getPort));
//            snowFlake.setModulo(servers.size()>0?servers.size():1);
//            return servers;
//        }
//    }
//
//}
